package net.cathackers.devel.scmp.auth.backends;

import net.cathackers.devel.scmp.auth.exceptions.ProxyException;
import net.cathackers.devel.scmp.auth.interfaces.AuthToken;
import net.cathackers.devel.scmp.auth.interfaces.Authenticator;

public class SimpleAuthToken implements AuthToken {
	
	private long tokenKey_;
	private long unixTimestamp;
	private boolean dirty_ = true;
	private Authenticator parentAuthenticator_;
	
	SimpleAuthToken(Authenticator parentAuthenticator, long tokenKey,long timeToLive) {
		setTokenKey(tokenKey);
		unixTimestamp = System.currentTimeMillis() + timeToLive;
		parentAuthenticator_ = parentAuthenticator;
	}
	
	private void setTokenKey(long tokenKey){
		tokenKey_ = tokenKey;
	}
	
	@Override
	public long getTokenKey() {
		return tokenKey_;
	}

	@Override
	public boolean isValid() throws ProxyException {
		boolean ret = false;
		if ( (System.currentTimeMillis() < unixTimestamp) & parentAuthenticator_.isAuthTokenValid(this)){
			ret = true;
		}
		return ret;
	}
	
	@Override
	public Authenticator getMyAuthenticator(){
		return parentAuthenticator_;
	}

	@Override
	public boolean isDirty() {
		return dirty_;
	}

	@Override
	public void syncObject() {
		// TODO sync the object to some storage
		dirty_ = false;
	}
}